package cn.sdormitory.evaluation.service.impl;

import cn.sdormitory.common.exception.ApiException;
import cn.sdormitory.common.utils.DataRoleUtil;
import cn.sdormitory.common.utils.SysUserUtils;
import cn.sdormitory.evaluation.dao.StudentHelpDao;
import cn.sdormitory.evaluation.dao.StudentHelpInfoDao;
import cn.sdormitory.evaluation.entity.StudentHelpEntity;
import cn.sdormitory.evaluation.entity.StudentHelpInfoEntity;
import cn.sdormitory.evaluation.entity.WorkDayEntity;
import cn.sdormitory.evaluation.service.StudentHelpService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class StudentHelpServiceImpl extends ServiceImpl<StudentHelpDao, StudentHelpEntity> implements StudentHelpService {

    @Resource
    StudentHelpInfoDao studentHelpInfoDao;
    @Resource
    DataRoleUtil dataRoleUtil;

    @Override
    public int saveStudentHelp(StudentHelpEntity entity) {
        //检查是否已经有帮扶记录
        LambdaQueryWrapper<StudentHelpEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentHelpEntity::getStudentId,entity.getStudentId());
        List<StudentHelpEntity> studentHelpEntities = this.baseMapper.selectList(wrapper);
        if(studentHelpEntities!=null && studentHelpEntities.size()>0){
            throw new ApiException("当前学生已经有 ["+studentHelpEntities.get(0).getHelpStudentName()+"] 帮扶");
        }
        entity.setCreateBy(SysUserUtils.getSysUser().getUsername());
        entity.setCreatedTime(new Date());
        int insert = this.baseMapper.insert(entity);
        return insert;
    }

    @Override
    public IPage<StudentHelpEntity> pageList(StudentHelpEntity vo) {
        LambdaQueryWrapper<StudentHelpEntity> wrapper = new LambdaQueryWrapper<>();
        //获取查询当前用户能查询到的范围
        List<String> userNameList = dataRoleUtil.getDeptSubordinateUserList();
        wrapper.in(userNameList.size()>0,StudentHelpEntity::getCreateBy, userNameList);

        wrapper.like(!StringUtils.isEmpty(vo.getStudentName()),StudentHelpEntity::getStudentName,vo.getStudentName());
        wrapper.orderBy(true,false,StudentHelpEntity::getCreatedTime);
        Page<StudentHelpEntity> page = page(new Page<>(vo.getPageNum(), vo.getPageSize()), wrapper);
        List<StudentHelpEntity> records = page.getRecords();
        for (StudentHelpEntity record : records) {
            StudentHelpInfoEntity info = new StudentHelpInfoEntity();
            LambdaQueryWrapper<StudentHelpInfoEntity> infoWrapper = new LambdaQueryWrapper<>();
            infoWrapper.eq(StudentHelpInfoEntity::getHelpId,record.getId());
            Integer integer = studentHelpInfoDao.selectCount(infoWrapper);
            record.setHelpNum(integer);
        }

        return page;
    }

    @Override
    public int addHelpInfo(StudentHelpInfoEntity vo) {
        if(vo==null || vo.getHelpId()==null || StringUtils.isEmpty(vo.getContext())){
            throw new ApiException("必要数据为空，请确认后提交");
        }
        vo.setCreateBy(SysUserUtils.getSysUser().getUsername());
        vo.setCreatedTime(new Date());
        int insert = this.studentHelpInfoDao.insert(vo);
        return insert;
    }

    @Override
    public List<StudentHelpInfoEntity> getHelpInfoListByHelpId(StudentHelpInfoEntity vo) {
        LambdaQueryWrapper<StudentHelpInfoEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentHelpInfoEntity::getHelpId,vo.getHelpId());
        wrapper.orderByDesc(StudentHelpInfoEntity::getCreatedTime);
        List<StudentHelpInfoEntity> studentHelpInfoEntities = studentHelpInfoDao.selectList(wrapper);
        return studentHelpInfoEntities;
    }
}
